package Team.Yaohuo.controller;


import Team.Yaohuo.constant.JDYConstant;
import Team.Yaohuo.properties.JwtProperties;
import Team.Yaohuo.properties.WxProperties;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@RestController
@Slf4j
public class WxController {



    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private WxProperties wxProperties;
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private HttpServletResponse httpServletResponse;
    @Autowired
    private HttpServletRequest httpServletRequest;

    /**
     * 获取企业微信鉴权校验
     * @param action 请求方法
     * @param code 可以根据此获得员工的userid
     * @param state 重定向后会带上state参数
     */
    @GetMapping("/getWxAuto")
    public void getWxAuto(@RequestParam(name = "action", defaultValue = "") String action,
                          @RequestParam(name = "code", defaultValue = "") String code,
                          @RequestParam(name = "state", defaultValue = "") String state) throws IOException {
        log.info("当前请求方法为:{}", action);
        log.info("当前用户身份信息为:{}", code);
        log.info("当前携带的state信息为:{}", state);
        // 获取企业微信access_token
        String url_accessToken = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+wxProperties.getCorpid()+"&corpsecret="+wxProperties.getCorpsecret();
        ResponseEntity<Map> result1 = restTemplate.exchange(url_accessToken, HttpMethod.GET, null, Map.class);
        String accessToken = (String) result1.getBody().get("access_token");
        log.info("开始获取access_token:{}", accessToken);
        // 获取userId
        String url_userId = "https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token="+accessToken+"&code="+code;
        ResponseEntity<Map> result2 = restTemplate.exchange(url_userId, HttpMethod.GET, null, Map.class);
        String userId = (String) result2.getBody().get("userid");
        log.info("开始获取userId:{}", userId);

        switch (userId) {
            case "suming":
                userId = "andy_wu";
                break;
            case "liang":
                userId = "liangyalin";
                break;
            case "liyingqing":
                userId = "chenyingqing";
                break;
        }

        HttpSession session = httpServletRequest.getSession();
        session.setAttribute("userId",userId);
        session.setAttribute("access_token", accessToken);
        httpServletResponse.sendRedirect(JDYConstant.ISS_JIANDAOYUN);
    }
}
